# pg.py -- interface to postgresql db, especially system tables.
import re

def table_names(db, namespace='public'):
    tables = db.select("""
        select relname from pg_class c
        inner join pg_namespace n on (c.relnamespace = n.oid and n.nspname='%s')
        where c.relkind='r'
        """ % namespace)
    return [t.relname for t in tables]
    
def make_tsquery(querystr):
    """Take a query string, clean it up, and return a string that can become a tsquery object in postgresql queries"""
    querystr = re.sub(u'\&', u' ', querystr, re.U)        # remove &
    terms = [t.strip() for t in querystr.split()]   # strip spaces                
    querystr = u' & '.join(terms)                   # join terms with &
    return querystr

